//
//  SliderView.m
//  HotWord
//
//  Created by 贺 欢 on 13-3-17.
//
//

#import "SliderView.h"
#import "SliderCell.h"
#import "UserInfoViewController.h"
#import "PeopleNearViewController.H"
#import "PeopleDreamSchoolViewController.h"
#import "InboxViewController.h"
#import "QAWallViewController.h"

@interface SliderView ()
{
    SliderOptionType _optionType;
}
@property (retain, nonatomic) IBOutlet UITableView *optionTableView;

@end

@implementation SliderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc
{
    [_optionTableView release];
    [super dealloc];
    
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    _optionType = SliderOptionTypeProfile;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SliderCell";
    SliderCell *cell = (SliderCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SliderCell" owner:self options:nil] objectAtIndex:0];
        UIView *background = [[UIView alloc] initWithFrame:cell.bounds];
        background.backgroundColor = RGBCOLOR(0, 130, 255);
        cell.selectedBackgroundView = background;
        [background release];
    }
    
    cell.optionType = _optionType;
    [cell setTitleWithIndex:indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == _optionType)
    {
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        [APP_DELEGATE showSliderView:NO];
        return;
    }
    
    _optionType = indexPath.row;
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    
    UINavigationController *navController = (UINavigationController *)APP_DELEGATE.window.rootViewController;
    [navController popToRootViewControllerAnimated:NO];
    
    UIViewController *viewController = nil;
    switch (_optionType)
    {
        case SliderOptionTypeProfile:
        {
            UserInfoViewController *userInfoViewController = [[[UserInfoViewController alloc] init] autorelease];
            viewController = userInfoViewController;
        }
            break;
        case SliderOptionTypeExplore:
            viewController = [self pushToExploreViewController];
            break;
        case SliderOptionTypeInbox:
        {
            InboxViewController * inboxViewController = [[[InboxViewController alloc] init] autorelease];
            viewController = inboxViewController;
        }
            break;
        case SliderOptionTypeQAWall:
        {
            QAWallViewController * wallViewController = [[[QAWallViewController alloc] init] autorelease];
            viewController = wallViewController;
        }
            break;
        case SliderOptionTypeBackToMain:
        {
            _optionType = SliderOptionTypeProfile;
            [_optionTableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.4];
        }
            break;
        default:
            break;
    }
    
    [navController pushViewController:viewController animated:NO];
    [APP_DELEGATE showSliderView:NO];

    
    if (_delegate && [_delegate respondsToSelector:@selector(sliderViewDidSelectOption:)])
    {
        [_delegate sliderViewDidSelectOption:_optionType];
    }
}

- (UIViewController *)pushToExploreViewController
{
    BOOL hasNoFilter = NO;
    BOOL hasNoMatchType = NO;
    
    if (IS_STRING_EMPTY([UserManager sharedManager].currentUser.match.matchType) || [@"0" isEqualToString:[UserManager sharedManager].currentUser.match.matchType])
    {
        hasNoFilter = YES;
        hasNoMatchType = YES;
    }
    
    if (IS_STRING_EMPTY([UserManager sharedManager].currentUser.exam))
    {
        hasNoFilter = YES;
    }
    
    if (![@"Student" isEqualToString:[UserManager sharedManager].currentUser.identity])
    {
        hasNoFilter = YES;
    }
    
    UIViewController *viewController = nil;
    if (hasNoFilter)
    {
        PeopleNearViewController * peopleNearViewController = [[[PeopleNearViewController alloc] init] autorelease];
        peopleNearViewController.hasNoFilter = YES;
        if (hasNoMatchType && [@"GRE" isEqualToString:[UserManager sharedManager].currentUser.exam] && [@"Student" isEqualToString:[UserManager sharedManager].currentUser.identity])
        {
            peopleNearViewController.hasGREMajor = YES;
        }
        viewController = peopleNearViewController;
    }
    else
    {
        PeopleDreamSchoolViewController * peopleDreamSchoolViewController = [[[PeopleDreamSchoolViewController alloc] init] autorelease];
        viewController = peopleDreamSchoolViewController;
    }
    
    return viewController;
}

@end
